package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.CommonViewController.CommonSearchTagActivity;
import com.mission.schedule.R;
import com.mission.schedule.activity.NewResiginActivity;
import com.mission.schedule.activity.PhoneCodeLoginActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.CalenderNoteBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.MyViewPager;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 清单编辑右侧菜单对话框
 * Created by liwei on 2017/4/18
 */

public class NoteEditRightMenuDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    TextView title,image;


    public NoteEditRightMenuDialog(@NonNull final Context context, @StyleRes final int themeResId,
                                   final WindowManager windowManager, final boolean istitle, final boolean isimage,
                                   final String userid, final String maxTitleId, final String titles, final String imgPath) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_note_edit_right_menu, null);
        setContentView(view);


        List<View> viewList = new ArrayList<View>();


        MyViewPager myViewPager = (MyViewPager) view.findViewById(R.id.viewpager_detail);

        View viewOne = LayoutInflater.from(context).inflate(R.layout.dialog_noteedit_detail_item1, null);
        View viewTwo = LayoutInflater.from(context).inflate(R.layout.dialog_noteedit_detail_item2, null);

        title = (TextView) viewOne.findViewById(R.id.addtitle);
        image = (TextView) viewOne.findViewById(R.id.addimage);

        if (istitle){
            title.setText("取消标题");
        }else {
            title.setText("插入标题");
        }

        if (isimage){
            image.setText("取消图片");
        }else {
            image.setText("插入图片");
        }

        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.title(istitle);
                dismiss();
            }
        });
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.image(isimage);
                dismiss();
            }
        });

        viewOne.findViewById(R.id.weixin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.shareweixin();
                dismiss();
            }
        });
        viewOne.findViewById(R.id.lianxiren).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.shareapp();
                dismiss();
            }
        });
        viewTwo.findViewById(R.id.add_daiban).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.add_daiban();
                dismiss();
            }
        });
        viewTwo.findViewById(R.id.add_richeng).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.add_richeng();
                dismiss();
            }
        });
        viewOne.findViewById(R.id.sendnote_url).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sendnote_url();
                dismiss();
            }
        });
        viewOne.findViewById(R.id.save_image).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.saveimage();
                dismiss();
            }
        });
        viewTwo.findViewById(R.id.set).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.set();
                dismiss();
            }
        });
        viewOne.findViewById(R.id.copyall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    callBack.copyall();
                } else {
                    VipDailog();
                }

                dismiss();
            }
        });
        viewOne.findViewById(R.id.copyallstyle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    callBack.copyllstyle();
                } else {
                    VipDailog();
                }

                dismiss();
            }
        });
        viewOne.findViewById(R.id.faxian).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.sharefaxian();
                dismiss();
            }
        });

        viewOne.findViewById(R.id.new_calender).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                RiLiDialog riLiDialog = new RiLiDialog(context, themeResId, windowManager);
                riLiDialog.show();
                riLiDialog.setCallBack(new RiLiDialog.CallBack() {
                    @Override
                    public void selectDate(String date) {
                        List<NoteTitleDetailBean.ListBean> listBeanList = App.getDBcApplication().getTitleNoteDetailData(userid, maxTitleId, false);
                        App.getDBcApplication().insertScheduleData(1,
                                titles.isEmpty() ? "无标题" : titles,
                                date, "23:59", 1, 0, 0, 1, 0, 0, 0,
                                date + " 23:59", "", 2, maxTitleId, "0", 0, "", "",
                                0, 0, 0, "完成任务", "g_88", "", 0, 0, 0, "", "", 0, 0, 0);

                        for (NoteTitleDetailBean.ListBean listBean: listBeanList) {
                            CalenderNoteBean.PageBean.ItemsBean itemsBean = new CalenderNoteBean.PageBean.ItemsBean();
                            itemsBean.titleId = maxTitleId;
                            itemsBean.uid = Integer.valueOf(userid);
                            itemsBean.id = App.getDBcApplication().getCalenderMinId() - 1;
                            itemsBean.style = listBean.style;
                            itemsBean.changeTime = DateUtil.nowTime();
                            itemsBean.createTime = DateUtil.nowTime();
                            itemsBean.ltype = listBean.lType;
                            itemsBean.orderId = listBean.orderId;
                            itemsBean.endstate = listBean.endstate;
                            itemsBean.remark = "";
                            itemsBean.contents = listBean.contents;
                            itemsBean.titles = titles.isEmpty() ? "无标题" : titles;
                            itemsBean.imgPath = imgPath;
                            itemsBean.imgUrl = "";
                            itemsBean.cpath = "";
                            itemsBean.curl = "";
                            itemsBean.sdesc = "";
                            itemsBean.nums = "";
                            itemsBean.localIds = listBean.id+"";
                            itemsBean.shareUrl = "";
                            itemsBean.remark1 = "";
                            itemsBean.remark2 = "";
                            itemsBean.remark3 = "";
                            itemsBean.remark4 = "";
                            itemsBean.remark5 = "";
                            itemsBean.remark6 = "";

                            App.getDBcApplication().saveCalederNoteDetailData(itemsBean);
                        }

                        Intent intent = new Intent(Const.SHUAXINDATA);
                        intent.putExtra("data", "success");
                        intent.putExtra("index", 1);
                        intent.putExtra("what", 2);
                        context.sendBroadcast(intent);

                        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });


        viewList.add(viewOne);
        viewList.add(viewTwo);

        ViewAdapter viewAdapter = new ViewAdapter(viewList);
        myViewPager.setAdapter(viewAdapter);

        final ImageView yd1 = (ImageView) view.findViewById(R.id.yd1);
        final ImageView yd2 = (ImageView) view.findViewById(R.id.yd2);

        myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                yd1.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd1));
                yd2.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd1));
                if (position == 0) {
                    yd1.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd2));
                }
                if (position == 1) {
                    yd2.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd2));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void VipDailog() {
        final android.app.AlertDialog builder = new android.app.AlertDialog.Builder(context).create();

        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setText("注册");
        delete_ok.setTextColor(Color.RED);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent newt = new Intent(context,PhoneCodeLoginActivity.class);
                newt.putExtra("type", 1);
                newt.putExtra("login",true);
                context.startActivity(newt);
            }
        });
        TextView cancle_tv = (TextView) window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("忽略");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);


        delete_tv.setText("该功能是正式用户功能\n请注册正式用户");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void title(boolean istitle);
        void image(boolean isimage);
        void shareweixin();
        void shareapp();
        void sharefaxian();
        void set();
        void saveimage();
        void copyall();
        void copyllstyle();
        void add_daiban();
        void add_richeng();
        void sendnote_url();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


    class ViewAdapter extends PagerAdapter {
        private List<View> viewList;//数据源

        public ViewAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    }
}
